package com.atguigu.gmall.product.controller.admin;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.SkuInfo;
import com.atguigu.gmall.mq.mqconst.Mqconst;
import com.atguigu.gmall.mq.service.RabbitService;
import com.atguigu.gmall.product.mapper.SkuInfoMapper;
import com.atguigu.gmall.product.service.SkuInfoService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin/product")
public class AdminSkuInfoController {

    @Autowired
    private SkuInfoService skuInfoService;

    @Autowired
    private RabbitService rabbitService;


    @PostMapping("/saveSkuInfo")
    public Result<Void> saveSkuInfo(@RequestBody SkuInfo skuInfo) {
        skuInfoService.saveSkuInfo(skuInfo);
        return Result.ok();
    }

    @GetMapping("/list/{pageNum}/{pageSize}")
    public Result<IPage<SkuInfo>> getSkuPage(@PathVariable("pageNum") Integer pageNum,
                                             @PathVariable("pageSize") Integer pageSize) {
        IPage<SkuInfo> skuInfoPage = new Page<>(pageNum, pageSize);
        IPage<SkuInfo> page = skuInfoService.page(skuInfoPage, null);
        return Result.ok(page);
    }

    @GetMapping("/onSale/{skuId}")
    public Result<Void> onSale(@PathVariable("skuId") Long skuId) {
        int isSale = 1;
        skuInfoService.onSale(skuId, isSale);
        rabbitService.sendMessage(Mqconst.RABBIT_EXCHANGE_GOODS, Mqconst.RABBIT_ROUTING_KEY_GOODS_IMPORT, skuId);
        return Result.ok();
    }

    @GetMapping("/cancelSale/{skuId}")
    public Result<Void> cancelSale(@PathVariable("skuId") Long skuId) {
        int isSale = 0;
        skuInfoService.cancelSale(skuId, isSale);
        rabbitService.sendMessage(Mqconst.RABBIT_EXCHANGE_GOODS, Mqconst.RABBIT_ROUTING_KEY_GOODS_REMOVE, skuId);
        return Result.ok();
    }
}
